package com.ruoyi.web.util;

import java.util.ArrayList;
import java.util.List;

import javax.xml.xpath.XPathConstants;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import cn.hutool.core.util.XmlUtil;

public class XmlReadUtilDemo {

	public static  List<Book> xmlToBean(String strXml, String nodePath, Book book) {
		Document docResult=XmlUtil.readXML(strXml);
		NodeList nodeList = XmlUtil.getNodeListByXPath(nodePath, docResult);
		System.out.println(nodeList.getLength());
		List<Book> bookList = new ArrayList<>();
		for (int i = 1; i < nodeList.getLength()+1; i++) {
			Book book_new = new Book();
			Object id =XmlUtil.getByXPath(nodePath +"["+i+"]/" + book.getGuid(), docResult, XPathConstants.STRING);
			book_new.setGuid(String.valueOf(id));
			Object valx =XmlUtil.getByXPath(nodePath +"["+i+"]/" + book.getName(), docResult, XPathConstants.STRING);
			book_new.setName(String.valueOf(valx));
			Object certificateNum =XmlUtil.getByXPath(nodePath +"["+i+"]/" + book.getCertificateNum(), docResult, XPathConstants.STRING);
			book_new.setCertificateNum(String.valueOf(certificateNum));
			
			Object title =XmlUtil.getByXPath(nodePath +"["+i+"]/" + book.getTitle(), docResult, XPathConstants.STRING);
			book_new.setTitle(String.valueOf(title));
			
			Object idnumber =XmlUtil.getByXPath(nodePath +"["+i+"]/" + book.getIDNumber(), docResult, XPathConstants.STRING);
			book_new.setIDNumber(String.valueOf(idnumber));
			
			bookList.add(book_new);
	}
		return bookList;
	}
	public static void main(String[] args) throws Exception {
		String strXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" + 
				"\r\n" + 
				"<PsDataPacket>\r\n" + 
				"  <RegistName/>\r\n" + 
				"  <AuditPer/>\r\n" + 
				"  <RegistNames>\r\n" + 
				"    <FDataList ItemType=\"HNPrjSupervise.ProspectDatum.Person, HNPrjSupervise.ProspectDatum\">\r\n" + 
				"      <Person>\r\n" + 
				"        <Guid>32ca5319-1a3f-466d-92d0-a8ff5444a35f</Guid>\r\n" + 
				"        <Name>孟灵寒</Name>\r\n" + 
				"        <CertificateNum>23452345544</CertificateNum>\r\n" + 
				"        <Title>一级工程师</Title>\r\n" + 
				"        <IDNumber>341204198010085784</IDNumber>\r\n" + 
				"      </Person>\r\n" + 
				"      <Person>\r\n" + 
				"        <Guid>7c07a7b7-7293-4baf-aef1-c88144add8d5</Guid>\r\n" + 
				"        <Name>卜白亦</Name>\r\n" + 
				"        <CertificateNum>634563</CertificateNum>\r\n" + 
				"        <Title>二级工程师</Title>\r\n" + 
				"        <IDNumber>460108199304018481</IDNumber>\r\n" + 
				"      </Person>\r\n" + 
				"    </FDataList>\r\n" + 
				"  </RegistNames>\r\n" + 
				"</PsDataPacket>";
		String nodePath = "/PsDataPacket/RegistNames/FDataList/Person";
		Book book = new Book();
		book.setGuid("Guid");
		book.setName("Name");
		book.setCertificateNum("CertificateNum");
		book.setTitle("Title");
		book.setIDNumber("IDNumber");
		List<Book> bookList = XmlReadUtilDemo.xmlToBean(strXml, nodePath, book);
		bookList.forEach(b->{
			System.out.println("============Start=================>");
			System.out.println(b.getGuid());
			System.out.println(b.getName());
			System.out.println(b.getCertificateNum());
			System.out.println(b.getTitle());
			System.out.println(b.getIDNumber());
			System.out.println("============End=================>");
		});
	}
	
	
}

class Book{
	private String Guid;
	private String Name;
	private String CertificateNum;
	private String Title;
	private String IDNumber;
	public String getGuid() {
		return Guid;
	}
	public void setGuid(String guid) {
		Guid = guid;
	}
	public String getName() {
		return Name;
	}
	public void setName(String name) {
		Name = name;
	}
	public String getCertificateNum() {
		return CertificateNum;
	}
	public void setCertificateNum(String certificateNum) {
		CertificateNum = certificateNum;
	}
	public String getTitle() {
		return Title;
	}
	public void setTitle(String title) {
		Title = title;
	}
	public String getIDNumber() {
		return IDNumber;
	}
	public void setIDNumber(String iDNumber) {
		IDNumber = iDNumber;
	}
	
	
	
}
